<?php
/**
 * Created by PhpStorm.
 * User: gqn
 * Date: 2016/3/14
 * Time: 14:45
 */

namespace Api\Controller;

class DrawMoneyController extends BaseController{

    public function index(){
        //解决跨域
        echoHeader();
        $user_id = I('post.user_id','');
        if($user_id == ''){
            $array = array("err_code"=>1003,'err_msg'=>'参数缺失');
            $this->ajaxReturn($array);
        }
        $userexist = M('user')->where("user_id = $user_id")->find();
        if(empty($userexist)){
            $array = array("err_code"=>1002,'err_msg'=>'该用户不存在');
            $this->ajaxReturn($array);
        }
        $exist = M('user')->field('draw_money_pass')->where("user_id = $user_id")->find();
        if($exist['draw_money_pass']!=''&& $exist['draw_money_pass']!=null){
            $array = array("err_code"=>200,'err_msg'=>'用户已经设置支付密码');
            $this->ajaxReturn($array);
        }else{
            $array = array("err_code"=>403,'err_msg'=>'用户未设置支付密码');
            $this->ajaxReturn($array);
        }

    }

    public function set(){
        //解决跨域
        echoHeader();
        $pass = trim(I('post.pass'));
        $user_id = trim(I('post.user_id'));
        $data = self::checkUserIsExist($user_id);
        if(!$data['user']){
            $this->ajaxReturn($data);
        }
        $data['user_id'] =$user_id;
        $usr = M('user')->field('random')->where($data)->find();
        $pass =  md5(md5($pass).$usr['random']);

        $data['draw_money_pass'] =$pass;

        $info = M('user')->save($data);
        if($info){
            $array = array("err_code"=>200,'err_msg'=>'增加成功！');
            $this->ajaxReturn($array);
        }else {
            $array = array("err_code" => 403, 'err_msg' => '增加失败！');
            $this->ajaxReturn($array);
        }
    }

    public function modify(){
        $pass = I('post.pass','');
        $new_pass = I('post.newpass','');
        $user_id = I('post.user_id','');
        $data = self::checkUserIsExist($user_id);
        if(!$data['user']){
            $this->ajaxReturn($data);
        }
        $data['user_id'] =$user_id;
        $usr = M('user')->field('random')->where($data)->find();
        $pass =  md5(md5($pass).$usr['random']);

        if($user_id ==''||$pass =='' || $new_pass==''){
            $array = array("err_code"=>1001,'err_msg'=>'参数缺失！');
            $this->ajaxReturn($array);
        }
        $redis = S(array('type' => 'redis'));
        $num = M('user')->field('draw_money_error_num')->where("user_id = $user_id")->find();
        $error = $num['draw_money_error_num'];
        if($error>='3'){
            $get = $redis->get($user_id);
            if(!$get){
                $map['draw_money_error_num'] = '0';
                $map['user_id'] = $user_id;
                M('user')->save($map);
            }else{
                $array = array("err_code" => 403, 'err_msg' => '密码已错误三次！请十五分钟后再试！');
                $this->ajaxReturn($array);
            }
        }
        $data = M('user')->field('draw_money_pass')->where("user_id = $user_id")->find();
        $old_pass = $data['draw_money_pass'];
        if($pass == $old_pass){
            $info['draw_money_pass'] =  md5(md5($new_pass).$usr['random']);
            $info['user_id'] = $user_id;
            $end= M('user')->save($info);
            if($end){
                $array = array("err_code"=>200,'err_msg'=>'修改成功！');
                $this->ajaxReturn($array);
            }else {
                $array = array("err_code" => 403, 'err_msg' => '修改失败！');
                $this->ajaxReturn($array);
            }
        }else{
            M('user')->where("user_id = $user_id")->setInc('draw_money_error_num',1);
            $num = M('user')->field('draw_money_error_num')->where("user_id = $user_id")->find();
            $error = $num['draw_money_error_num'];
            if($error>='3'){
                $array = array("err_code" => 403, 'err_msg' => '密码已错误三次！请十五分钟后再试！');
                $redis->set($user_id,1);
                $redis->expire($user_id,900);
                $this->ajaxReturn($array);
            }
            $array = array("err_code" => 404, 'err_msg' => '原密码错误！');
            $this->ajaxReturn($array);
        }

    }




}